@echo off
chcp 65001 >nul
echo =========================================
echo    GMCM 2025 Problem 1 Quick Starter
echo =========================================
echo Always performs complete rebuild from source
echo.

echo === Complete Rebuild Process ===
echo.

REM Step 1: Clean all previous build artifacts
echo [1/4] Cleaning previous build artifacts...
if exist "competition_scheduler.exe" del "competition_scheduler.exe"
if exist "main_competition.o" del "main_competition.o"
if exist "graph_core.o" del "graph_core.o"
if exist "csv_loader.o" del "csv_loader.o" 
if exist "optimizer.o" del "optimizer.o"
echo     ✅ Old files cleaned
echo.

REM Step 2: Compile each source file to object file
echo [2/4] Compiling source files to object files...
echo     Compiling main_competition.c...
gcc -std=c99 -O2 -Wall -Wextra -c main_competition.c -o main_competition.o
if %ERRORLEVEL% neq 0 goto :compile_error

echo     Compiling graph_core.c...
gcc -std=c99 -O2 -Wall -Wextra -c graph_core.c -o graph_core.o  
if %ERRORLEVEL% neq 0 goto :compile_error

echo     Compiling csv_loader.c...
gcc -std=c99 -O2 -Wall -Wextra -c csv_loader.c -o csv_loader.o
if %ERRORLEVEL% neq 0 goto :compile_error

echo     Compiling optimizer.c...
gcc -std=c99 -O2 -Wall -Wextra -c optimizer.c -o optimizer.o
if %ERRORLEVEL% neq 0 goto :compile_error
echo     ✅ All object files compiled successfully
echo.

REM Step 3: Link object files into final executable
echo [3/4] Linking object files into executable...
gcc -std=c99 -O2 -o competition_scheduler main_competition.o graph_core.o csv_loader.o optimizer.o -lm
if %ERRORLEVEL% neq 0 goto :compile_error
echo     ✅ Executable linked successfully
echo.

REM Step 4: Verify final executable
echo [4/4] Verifying executable...
if not exist "competition_scheduler.exe" (
    echo     ❌ ERROR: Executable not found after compilation!
    goto :compile_error
)
echo     ✅ Final executable verified
echo.
echo === Complete Rebuild Successful ===
echo All .o files and .exe file freshly generated from source
echo.

echo Starting high-quality optimization for Conv_Case0...
echo This may take 30+ seconds for best results.
echo.

REM Run the competition scheduler
.\competition_scheduler.exe

echo.
echo =========================================
echo           Optimization Complete!
echo =========================================
echo.
echo Results saved in: result\Problem1\
echo - Conv_Case0_schedule.txt (Competition submission file)
echo - Conv_Case0_summary.txt  (Optimization summary)
echo.

REM Check if results exist and show summary
if exist "result\Problem1\Conv_Case0_summary.txt" (
    echo === OPTIMIZATION SUMMARY ===
    type "result\Problem1\Conv_Case0_summary.txt"
    echo.
    echo ✅ SUCCESS: Results ready for competition submission!
) else (
    echo ❌ ERROR: Results not found. Please check for errors above.
)

echo.
echo Press any key to exit...
pause >nul
goto :end

:compile_error
echo.
echo ❌ ERROR: Compilation failed!
echo Please check your code for errors.
pause
exit /b 1

:end